import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "../components/Main.vue"
import ForgetPassword from "../components/ForgetPassword.vue"
import Login from "../components/Login.vue"
import Register from "../components/Register.vue"
import Comment from "../components/Comment.vue"
import UserManage from "../components/UserManage.vue"
import HouseResources from "../components/HouseResources.vue"
import News from "../components/News.vue"


Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login',meta:{title:"好来屋登录页面"}},
  {path:"/main",component:Main,meta:{title:"好来屋后台主页"},children:[
    {path:"/comment",component:Comment,meta:{title:"好来屋后台主页"}},
    {path:"/userManage",component:UserManage,meta:{title:"好来屋后台主页"}},
    {path:"/houseResources",component:HouseResources,meta:{title:""}},
    {path:"/news",component:News,meta:{title:""}}
  ]},
  {path:"/forgetPassword",component:ForgetPassword,meta:{title:"忘记密码页面"}},
  {path:"/login",component:Login,meta:{title:"好来屋登录页面"}},
  {path:"/register",component:Register,meta:{title:"好来屋注册页面"}}
]


const router = new VueRouter({
  routes
})


//添加路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path==="/register"||to.path==="/forgetPassword"||to.path==="/login"){
    return next()
  }
  let token=window.sessionStorage.getItem("token")
  if(token!==null&&token.length>0){
    return next()
  }
  next("/login")
})


export default router
